<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="菜单名称" prop="menuName">
        <el-input v-model="queryParams.menuName" placeholder="请输入菜单名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="菜单状态" prop="menuState">
        <el-select v-model="queryParams.menuState" placeholder="请选择菜单状态" clearable>
          <el-option v-for="dict in bk_menu_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['bk:menu:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['bk:menu:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['bk:menu:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['bk:menu:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="menuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="菜单id" align="center" prop="menuId" />
      <el-table-column label="菜单名称" align="center" prop="menuName" />
      <el-table-column label="文件路径" align="center" prop="menuComponent" />
      <el-table-column label="菜单地址" align="center" prop="menuUrl" />
      <el-table-column prop="menuIcon" label="图标" align="center" width="100">
        <template #default="scope">
          <svg-icon :icon-class="scope.row.menuIcon" />
        </template>
      </el-table-column>
      <el-table-column label="显示顺序" align="center" prop="menuOrder" />
      <el-table-column label="菜单状态" align="center" prop="menuState">
        <template #default="scope">
          <dict-tag :options="bk_menu_status" :value="scope.row.menuState" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['bk:menu:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['bk:menu:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改菜单信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="menuRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="form.menuName" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="菜单地址" prop="menuUrl">
          <el-input v-model="form.menuUrl" placeholder="请输入菜单地址" />
        </el-form-item>
        <el-form-item label="文件路径" prop="menuComponent">
          <el-input v-model="form.menuComponent" placeholder="请输入路径" />
        </el-form-item>
        <el-form-item label="直接输入图标或选择" prop="menuIcon">
          <el-input v-model="form.menuIcon" placeholder="请输入图标名称" />
        </el-form-item>
        <el-col :span="12" v-if="form.menuType != 'F'">
          <el-form-item label="菜单图标" prop="menuIcon">
            <el-popover placement="bottom-start" :width="540" trigger="click">
              <template #reference>
                <el-input v-model="form.menuIcon" placeholder="点击选择图标" @blur="showSelectIcon" readonly>
                  <template #prefix>
                    <svg-icon v-if="form.menuIcon" :icon-class="form.menuIcon" class="el-input__icon"
                      style="height: 32px;width: 16px;" />
                    <el-icon v-else style="height: 32px;width: 16px;">
                      <search />
                    </el-icon>
                  </template>
                </el-input>
              </template>
              <icon-select ref="iconSelectRef" @selected="selected" :active-icon="form.menuIcon" />
            </el-popover>
          </el-form-item>
        </el-col>
        <el-form-item label="显示顺序" prop="menuOrder">
          <el-input-number v-model="form.menuOrder" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="菜单状态" prop="menuState">
          <el-radio-group v-model="form.menuState">
            <el-radio v-for="dict in bk_menu_status" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Menu">
import { listMenu, getMenu, delMenu, addMenu, updateMenu } from "@/api/bk/menu";
import SvgIcon from "@/components/SvgIcon";
import IconSelect from "@/components/IconSelect";

const { proxy } = getCurrentInstance();
const { bk_menu_status } = proxy.useDict('bk_menu_status');

const menuList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const iconSelectRef = ref(null);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    menuName: null,
    menuState: null,
  },
  rules: {
    menuName: [
      { required: true, message: "菜单名称不能为空", trigger: "blur" }
    ],
    menuComponent: [
      { required: true, message: "文件路径不能为空", trigger: "blur" }
    ],
    menuUrl: [
      { required: true, message: "菜单地址不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 展示下拉图标 */
function showSelectIcon() {
    iconSelectRef.value.reset();
}
/** 选择图标 */
function selected(name) {
    form.value.menuIcon = name;
}

/** 查询菜单信息列表 */
function getList() {
  loading.value = true;
  listMenu(queryParams.value).then(response => {
    menuList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    // 排序
    menuList.value.sort((a, b) => a.menuOrder - b.menuOrder);
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    menuId: null,
    menuName: null,
    menuUrl: null,
    menuIcon: null,
    menuOrder: null,
    menuState: null,
    createTime: null,
    updateTime: null,
    menuComponent: null
  };
  proxy.resetForm("menuRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.menuId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加菜单信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _menuId = row.menuId || ids.value
  getMenu(_menuId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改菜单信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["menuRef"].validate(valid => {
    if (valid) {
      if (form.value.menuId != null) {
        updateMenu(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMenu(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _menuIds = row.menuId || ids.value;
  proxy.$modal.confirm('是否确认删除菜单信息编号为"' + _menuIds + '"的数据项？').then(function () {
    return delMenu(_menuIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('bk/menu/export', {
    ...queryParams.value
  }, `menu_${new Date().getTime()}.xlsx`)
}

getList();
</script>
